package com.jwdf.uda.ui.activity.presenter;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.PbBean;
import com.jwdf.uda.bean.UploadImage;
import com.jwdf.uda.event.GuideDetailEvent;
import com.jwdf.uda.http.AHttpService;
import com.jwdf.uda.http.HttpHelper;
import com.jwdf.uda.http.action.GuideAddCommentAction;
import com.jwdf.uda.ui.activity.view.IGuideCommentAddView;
import com.jwdf.uda.utils.EventBusUtil;
import com.jwdf.uda.utils.StringUtils;
import com.jwdf.uda.utils.ToastUtils;

import java.util.List;

import retrofit2.Response;

/**
 * 类说明：
 */
public class GuideCommentAddPresenter extends AUploadImagePresenter<IGuideCommentAddView> {
    private int mGuideId;
    private int mAtCommentId;

    public GuideCommentAddPresenter(IGuideCommentAddView iView, int guideId, int atCommentId) {
        super(iView);
        mGuideId = guideId;
        mAtCommentId = atCommentId;
    }

    public void onHeadActionClick() {
        String content = mIView.getContent();
        List<UploadImage> uploadImages = getUploadImageList();
        if (StringUtils.isNullEmpty(content) && (uploadImages == null || uploadImages.size() <= 0)) {
            ToastUtils.show(getContext(), R.string.guide_comment_add_hint);
            return;
        }
        mIView.getFragment().showProgressDialog();
        if(uploadImages != null && uploadImages.size() > 0){
            doUploadImagesAction();
        }else{
            doGuideCommentAddAction(getImageTokenList());
        }
    }


    @Override
    protected List<UploadImage> getUploadImageList() {
        return mIView.getUploadImageList();
    }

    @Override
    protected void doUploadImage(UploadImage uploadImage) {
        mIView.setImageToken(uploadImage);
    }

    @Override
    protected void doUploadImagesSuccess(List<String> imageTokens) {
        doGuideCommentAddAction(imageTokens);
    }

    public void doGuideCommentAddAction(List<String> imageTokens) {
        GuideAddCommentAction action = GuideAddCommentAction.newInstance(getContext(), mGuideId, mIView.getContent(), mAtCommentId, imageTokens, PbBean.PbSignNavType.LINK_TYPE_GUIDE);
        mIView.getFragment().onNewRequestCall(action.request(new AHttpService.IResCallback<PbBean.PbResCommentAddComment>() {
            @Override
            public void onCallback(int resultCode, Response<PbBean.PbResCommentAddComment> response, PbBean.PbBaseServerInfo baseServerInfo) {
                if (mIView.getFragment().getView() != null) {
                    mIView.getFragment().closeProgressDialog();
                    if (HttpHelper.showError(getContext(), resultCode, baseServerInfo, R.string.error_operation)) {
                        return;
                    }

                    doGuideCommentAddSuccess(response.body());
                }
            }
        }));
    }

    private void doGuideCommentAddSuccess(PbBean.PbResCommentAddComment body) {
        mIView.getFragment().getActivity().finish();
        EventBusUtil.post(new GuideDetailEvent(GuideDetailEvent.EVENT_COMMENT_ADD));
    }
}
